package cos.titlegetter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.qotsa.exception.InvalidHandle;
import com.qotsa.jni.controller.WinampController;

import cos.entity.Song;

public class DllTitleGetter implements TitleGetter {

	final Pattern p = Pattern.compile("\\d+\\. (.+) - (.+) - ");

	public Song getCurrentSongTitle() throws InvalidHandle {
		return parseDllResponse(WinampController.getTitle());
	}

	// parse string, trying to ger pure Artist name and Song title
	private Song parseDllResponse(String line) {
		if (line == null)
			return null;

		final Matcher m = p.matcher(line);
		while (m.find()) {
			final String artist = m.group(1);
			final String title = m.group(2);
			if (artist != null && title != null)
				return new Song(artist, title);
		}
		return null;
	}
}
